package org.example.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.commonapi.domain.R;
import org.example.commonapi.domain.School;
import org.example.commonapi.domain.User;
import org.example.commonapi.dto.LoginInfo;
import org.example.commonapi.util.JwtUtil;
import org.example.user.service.SchoolService;
import org.example.user.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class SchoolController {

    private static final Logger log = LoggerFactory.getLogger(SchoolController.class);
    @Resource
    private JwtUtil jwtUtil;

    @Resource
    private UserService userService;

    @Resource
    private SchoolService schoolService;

    /**
     * 获取用户所在学校信息
     * @param request
     * @return
     */
    @GetMapping("/get/school")
    public R getSchool(HttpServletRequest request){
        LoginInfo loginInfo=jwtUtil.parseJwt(request.getHeader("token"));
        //查询用户信息
        LambdaQueryWrapper<User> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getId,loginInfo.getId());
        User user=userService.getOne(queryWrapper);
        log.info("当前用户:{}",user);
//        log.info("用户的学校id{}",user.getSchoolId());
        School school=schoolService.getById(user.getSchoolId());
        log.info("用户所在学校学校:{}",school);
        return R.success(school);
    }


    /**
     * 获取所有学校的信息
     * @return
     */
    @GetMapping("/list/school")
    public R listSchool(String schoolName) {
        List<School> list=schoolService.listSchool(schoolName);
        return R.success(list);
    }
}
